<?php

namespace App\Trip\Traits\TimeTraits;

use Carbon\Carbon;
use Exception;

trait DepartDateTimeTrait
{
    /**
     * 是否发车
     *
     * @return bool
     * @throws Exception
     */

    public function isDepart(): bool
    {
        if (!$this->getDepartAt()) {
            throw new Exception("出发日期格式错误");
        }
        return Carbon::parse($this->getDepartAt())->lessThan(Carbon::now());
    }

    /**
     * 完整出发日期时间
     *
     * @return string
     */
    public function getDepartAt(): string
    {
        return $this->depart_at;
    }

    /**
     * @throws Exception
     */
    public function setDepartAt(string $start_at): void
    {
        if (!$this->isDatetime($start_at)) {
            throw new Exception('出发日期格式错误');
        }
        $this->depart_at = $start_at;
    }


    public function getStartDate(): string
    {
        return Carbon::parse($this->getDepartAt())->toDateString();
    }

    public function getStartTime(): string
    {
        return Carbon::parse($this->getDepartAt())->format("H:i");
    }
}